home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 26 / macformat_26.iso / Shareware / Programación / C Reference Card / C Reference Card.rsrc / TEXT_407_7.txt < prev    next >
Text File  |  1997-01-29  |  5KB  |  139 lines

  1. STRUCTURES : defining, accessing, unions, fields, linked lists
  2. _________________________________________________________________________
  3.  
  4.  
  5. Structures, also known as "records", provide the means to group related data of different types together in a convenient package.  Structures also represent the basic building blocks of C++ classes.
  6.  
  7.  
  8. DEFINING STRUCTURES
  9.  
  10. A structure definition consists of the 'struct' keyword and the structure name followed by a list of variable declarations:
  11.  
  12.   struct tag
  13.   {
  14.     variable declarations
  15.   } optional_struct_declaration;
  16.  
  17. The following is an example of a structure definition:
  18.  
  19.   struct employee
  20.   {
  21.     char *lastName;
  22.     char *firstName;
  23.     char *streetAddress;
  24.     char *city;
  25.     char *state;
  26.     long zipCode;
  27.     int  employeeID;
  28.     int  age;
  29.   };
  30.  
  31. The above definition only defines a structure of type 'employee'.  To declare a variable which is a structure of type 'employee', the following declaration is used:
  32.  
  33.   struct employee person; // declares and allocates memory for structure
  34.  
  35. Many times, pointers to structures are used.
  36.  
  37.   struct employee *personPtr; // declares pointer only
  38.  
  39. If your working with pointers to structures, you must follow your pointer variable declaration with code to allocate memory for the structure.  If your using straight C, you'd use 'malloc' from the ANSI library to allocate the memory for the structure like this:
  40.  
  41.   personPtr = (struct employee *)malloc( sizeof( struct employee ) );
  42.  
  43. With C++, you use the 'new' operator to allocate the memory as follows:
  44.  
  45.   personPtr = new employee;
  46.  
  47. The C++ way is obviously a better way of allocating memory and should be used in place of the old 'malloc' command.
  48.  
  49.  
  50.  
  51. ACCESSING STRUCTURE VARIABLES
  52.  
  53. If you've declared a structure variable, you access the variable members using the '.' member operator like this:
  54.  
  55.   person.employeeID = 10;
  56.   person.age = 35;
  57.  
  58. You can use structure members just like any other variable.
  59.  
  60.   if( person.age > 65 )
  61.     DoSendPinkSlip( person );
  62.  
  63. When using pointers to structures, you access the variable members using the '->' member operator in a similar manner.
  64.  
  65.   personPtr->employeeID = 10;
  66.   personPtr->age = 35;
  67.  
  68.  
  69.  
  70. UNIONS
  71.  
  72. Unions allow you to store different data type in the same memory location (but not at the same time).  They follow the same syntax as structures:
  73.  
  74.   union optional_tag
  75.   {
  76.     variable declarations
  77.   } optional_union_declaration;
  78.  
  79. The following is an example of a union definition:
  80.  
  81.   union myUnionDef
  82.   {
  83.     int   integer;
  84.     float real;
  85.     char  *string;
  86.   } myUnion;
  87.  
  88. The memory allocated for a union will be large enough to hold the largest item declared in the union definition.  Unions might be a good idea for maximizing memory use, but generally, they are rarely used and should probably be avoided.
  89.  
  90.  
  91.  
  92. BIT FIELDS
  93.  
  94. Fields, like unions, help to maximize memory usage but in days where memory is cheap and code clarity is important, fields are used less and less.  They are implementation-dependent and create cross platform compatibility problems.  Here's an example of a field definition:
  95.  
  96.   struct sysFlags
  97.   {
  98.     unsigned int SysFlag1 : 1;  
  99.     unsigned int SysFlag2 : 1;
  100.     unsigned int SystemID : 2;
  101.     unsigned int          : 4;  // unused
  102.     unsigned int SysFlag3 : 1;
  103.   };
  104.  
  105.  
  106.  
  107. LINKED LISTS
  108.  
  109. Linked lists are a series of structures which are usually dynamically allocated and linked together (by pointers) to maintain a bond.  Consider the following structure definition:
  110.  
  111.   struct student
  112.   {
  113.     struct student *next;
  114.     char           *name;
  115.     int            id;
  116.   };
  117.  
  118. When defining a new student, you would "link" the student to the existing list of students by doing something like this:
  119.  
  120.   struct student *gTopOfList;    // In this example, program keeps track
  121.   struct student *gEndOfList;    // of top and bottom of linked list.
  122.   struct student *freshman;
  123.  
  124.   freshman = new student;
  125.  
  126.   gEndOfList->next = freshman;   // Last student is now new freshman.
  127.   gEndOfList = freshman;         // Update global variable.
  128.   gEndOfList->next = nil;        // Identifies end of list.
  129.  
  130. Using this example, you could print the entire student roster with the following code fragment:
  131.  
  132.   struct student *currentStudent;
  133.  
  134.   currentStudent = gTopOfList;
  135.   do{
  136.     cout << currentStudent->name << endl;
  137.   } while( currentStudent->next != nil );
  138.  
  139. An improved linked list would include a "prev" pointer in addition to the "next" pointer.  This allows you to travel up as well as down the linked list chain of structures.  There are tree linked lists, circular linked lists, and quite possibly, an infinite number of types and combinations you could come up with.